package com.yf.dxapp.module.app.sms;

import cn.hutool.http.HttpUtil;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import com.alibaba.fastjson.JSONObject;
import com.yf.dxapp.module.app.consts.MyConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class ClSmsHandler implements SmsHandler{

    private String account = "YZM442342_YZM4752247";

    private String password = "I5m12uTkEB8657";



    public void senCaptcha(String phone,String msg){
        String sendUrl = "https://smssh1.253.com/msg/v1/send/json";
        Map map = new HashMap();
        map.put("account",account);//API账号
        map.put("password",password);//API密码
        String format = "【鑫莘学苑】您的验证码为:%s，请勿泄露于他人";
        format = format.formatted(msg);
        map.put("msg",format);//手机号
        map.put("phone",phone);//手机号
        map.put("report","true");//是否需要状态报告
//        map.put("extend","123");//自定义扩展码
        JSONObject js = (JSONObject) JSONObject.toJSON(map);
        String res = HttpUtil.post(sendUrl,js.toString());
        log.error("ChuangLan短信结果:{},{}",phone,res);
        JSONObject result = JSONObject.parseObject(res);
        if(!result.getString("code").equals("0")){
            throw new ServiceException(MyConst.BUS_ERROR_CODE,"短信发送失败");
        }
    }

}
